{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DataFrame是什么\n",
    "DataFrame：二维的表格型数据结构。很多功能与R中的data.frame类似。  \n",
    "可以将DataFrame理解为Series的容器。以下的内容主要以DataFrame为主。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建DataFrame\n",
    "通过给pd.DataFrame()方法传入不同的对象来创建DataFrame。\n",
    "\n",
    "## 传入列表对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0\n",
      "0  a\n",
      "1  b\n",
      "2  c\n",
      "3  d\n",
      "4  e\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "str_list = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
    "DF21 = pd.DataFrame(str_list)\n",
    "print(DF21)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 传入嵌套列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "      <td>!</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>6</td>\n",
       "      <td>*</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>h</td>\n",
       "      <td>8</td>\n",
       "      <td>@</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>k</td>\n",
       "      <td>12</td>\n",
       "      <td>#</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0   1  2\n",
       "0  a   1  !\n",
       "1  b   6  *\n",
       "2  h   8  @\n",
       "3  k  12  #"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 方式1\n",
    "list_1 = [\"a\", \"1\", \"!\"]\n",
    "list_2 = [\"b\", \"6\", \"*\"]\n",
    "list_3 = [\"h\", \"8\", \"@\"]\n",
    "list_4 = [\"k\", \"12\", \"#\"]\n",
    "\n",
    "DF22 = pd.DataFrame([list_1, list_2, list_3, list_4])\n",
    "DF22"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 指定行、列索引\n",
    "自定义行索引: index参数；  \n",
    "自定义列索引：columns参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>字母</th>\n",
       "      <th>数字</th>\n",
       "      <th>字符</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "      <td>!</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>b</td>\n",
       "      <td>6</td>\n",
       "      <td>*</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>h</td>\n",
       "      <td>8</td>\n",
       "      <td>@</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>four</th>\n",
       "      <td>k</td>\n",
       "      <td>12</td>\n",
       "      <td>#</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      字母  数字 字符\n",
       "one    a   1  !\n",
       "two    b   6  *\n",
       "three  h   8  @\n",
       "four   k  12  #"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "list_1 = [\"a\", \"1\", \"!\"]\n",
    "list_2 = [\"b\", \"6\", \"*\"]\n",
    "list_3 = [\"h\", \"8\", \"@\"]\n",
    "list_4 = [\"k\", \"12\", \"#\"]\n",
    "\n",
    "DF23 = pd.DataFrame([list_1, list_2, list_3, list_4],\n",
    "                   index=[\"one\",\"two\",\"three\",\"four\"],\n",
    "                   columns=[\"字母\", \"数字\", \"字符\"])\n",
    "DF23"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 传入字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>字母</th>\n",
       "      <th>数字</th>\n",
       "      <th>字符</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>一</th>\n",
       "      <td>a</td>\n",
       "      <td>7</td>\n",
       "      <td>$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>二</th>\n",
       "      <td>f</td>\n",
       "      <td>5</td>\n",
       "      <td>*</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>三</th>\n",
       "      <td>c</td>\n",
       "      <td>3</td>\n",
       "      <td>@</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  字母 数字 字符\n",
       "一  a  7  $\n",
       "二  f  5  *\n",
       "三  c  3  @"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data_dict = {\"字母\": [\"a\", \"f\", \"c\"], \"数字\": [\"7\", \"5\", \"3\"], \"字符\": [\"$\", \"*\", \"@\"]}\n",
    "data_dict_DF = pd.DataFrame(data_dict,\n",
    "                            index=[\"一\", \"二\", \"三\"])\n",
    "data_dict_DF"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取DataFrame的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>字母</th>\n",
       "      <th>数字</th>\n",
       "      <th>字符</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>一</th>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "      <td>!</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>二</th>\n",
       "      <td>b</td>\n",
       "      <td>B</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>三</th>\n",
       "      <td>3</td>\n",
       "      <td>C</td>\n",
       "      <td>@</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>四</th>\n",
       "      <td>4</td>\n",
       "      <td>F</td>\n",
       "      <td>#</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  字母 数字 字符\n",
       "一  a  1  !\n",
       "二  b  B  2\n",
       "三  3  C  @\n",
       "四  4  F  #"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "list_1 = [\"a\", \"1\", \"!\"]\n",
    "list_2 = [\"b\", \"B\", \"2\"]\n",
    "list_3 = [\"3\", \"C\", \"@\"]\n",
    "list_4 = [\"4\", \"F\", \"#\"]\n",
    "\n",
    "DF3 = pd.DataFrame([list_1, list_2, list_3, list_4],\n",
    "                   columns=[\"字母\", \"数字\", \"字符\"],\n",
    "                   index=[\"一\", \"二\", \"三\", \"四\"])\n",
    "DF3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['一', '二', '三', '四'], dtype='object')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取行索引\n",
    "DF3.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['字母', '数字', '字符'], dtype='object')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取列索引\n",
    "DF3.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取DataFrame的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['a', '1', '!'],\n",
       "       ['b', 'B', '2'],\n",
       "       ['3', 'C', '@'],\n",
       "       ['4', 'F', '#']], dtype=object)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "list_1 = [\"a\", \"1\", \"!\"]\n",
    "list_2 = [\"b\", \"B\", \"2\"]\n",
    "list_3 = [\"3\", \"C\", \"@\"]\n",
    "list_4 = [\"4\", \"F\", \"#\"]\n",
    "\n",
    "DF4 = pd.DataFrame([list_1, list_2, list_3, list_4],\n",
    "                   columns=[\"字母\", \"数字\", \"字符\"],\n",
    "                   index=[\"一\", \"二\", \"三\", \"四\"])\n",
    "\n",
    "\n",
    "DF4.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查看不同列的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "字母    object\n",
       "数字    object\n",
       "字符    object\n",
       "dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DF3.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
